
/**********************下拉搜索框**********************************************/
document.getElementById("stoke_num").addEventListener('input', function() {
	var arr = [];
	var val = this.value;
	var val = jQuery(this).val();
	jQuery("#searchRes").empty();
	if(val == '') {
		jQuery("#searchRes").empty();
	} else {
		jQuery('#scroll').show();
		//document.getElementById("scroll").style.display='block';
		for(var i = 0, resArr = []; i < list.length; i++) {
			if(list[i].code.indexOf(val) != -1) {
				//console.log('包含--'+list[i].code)
				var src = '';
				if(list[i].type == 'HK') {
					src = 'images/HK_20x15_@3x.png';
				} else {
					src = 'images/US_40_29.png';
				}
				jQuery("#searchRes").append("<ul class='mui-row'><li class='mui-col-sm-2 mui-col-xs-2'><img src='" + src + "' alt='' class='mui-pull-left'></li><li class='mui-col-sm-4 mui-col-xs-4'><span>" + list[i].code + "</span></li><li style='text-align: right;' class='mui-col-sm-6 mui-col-xs-6'><span style='margin-right:10px;'>" + list[i].name + "</span></li></ul>");
			}
		}
		mui('.mui-scroll-wrapper').scroll({
			scrollY: true, //是否竖向滚动
			scrollX: false, //是否横向滚动
			startX: 0, //初始化时滚动至x
			startY: 0, //初始化时滚动至y
			indicators: true, //是否显示滚动条
			deceleration: 0.0006, //阻尼系数,系数越小滑动越灵敏
			bounce: true //是否启用回弹
		});
	}

});
mui("#searchRes").on('tap', 'ul', function() {
	var i = jQuery(this).index();
	// alert(i);
	var val = $(this).find("li:nth-child(2)").text(); //注意：这里的下标从一开始，或者使用:first-child  
	document.getElementById("stoke_num").value = val;

	document.getElementById("scroll").style.display = 'none';
	mui.ajax(server.stockOne, {
		data: {
			stockCode: val
		},
		dataType: 'json', //数据格式类型
		type: 'POST', //http请求类型
		timeout: 10000, //超时设置
		success: function(data) {
			//处理成功返回的数据
			console.log(JSON.stringify(data));
			var name = data.data[0].name;
			var change = data.data[0].change;
			var changeRate = data.data[0].changeRate;
			var high = data.data[0].high;
			var low = data.data[0].low;
			var openPrice = data.data[0].openPrice;
			var closePrice = data.data[0].closePrice;
			var a = 0.1

			var h = closePrice * a;
			var price = data.data[0].price;
			var h = closePrice + h;
			var l = closePrice - h;

			//alert(h);
			document.getElementById("p1").innerHTML = name;
			document.getElementById("p2").innerHTML = change;
			document.getElementById("p3").innerHTML = changeRate + "%";
			document.getElementById("hi").innerHTML = h;
			document.getElementById("lo").innerHTML = l;
			document.getElementById("mony").value = price;

		}
	})

});

mui('.mui-scroll-wrapper').scroll({
	scrollY: true, //是否竖向滚动
	scrollX: false, //是否横向滚动
	startX: 0, //初始化时滚动至x
	startY: 0, //初始化时滚动至y
	indicators: false, //是否显示滚动条
	deceleration: 0.0005, //阻尼系数,系数越小滑动越灵敏
	bounce: true //是否启用回弹
});

mui('.mui-bar-nav').on('tap', '.optionalEdit', function() {
	mui.openWindow({
		url: 'optionalEdit.html'
	});
});
/*******************导航切换*************************/
mui("#deal_title").on('tap', 'li', function() {
	var i = jQuery(this).index();
	jQuery(".part").eq(i).show();
	jQuery(".part").eq(i).siblings(".part").hide();
	jQuery(".active").removeClass("active");
	jQuery(this).addClass("active");
	if(i==1){
		/**********************撤单****************************/
		jQuery.ajax({
			type: 'post',
			url: server.stockDay,
			data: {
			account: account,
			billStatus:2
			},
			dataType: 'json', //数据格式类型
			success: function(data) {
				//处理成功返回的数据
		//console.log(JSON.stringify(data));
		for(i = 0; i < data.data.length; i++) {
			var id = data.data[i].id;
			var codeName = data.data[i].codeName;
			var stockCode = data.data[i].stockCode;
			var count = data.data[i].count;
			var floatFee = data.data[i].floatFee;
			var floatRatio = data.data[i].floatRatio;
			var occupyDeposit = data.data[i].occupyDeposit;
			var countCost = data.data[i].countCost;
			var newPrice = data.data[i].newPrice;
			var currency = data.data[i].currency;
			var count = data.data[i].count;
			var floatFee = data.data[i].floatFee;
			document.getElementById("price1").innerHTML = floatFee;
			document.getElementById("price2").innerHTML = floatFee;
			document.getElementById("price3").innerHTML = floatFee;
			jQuery("#day_lis").append("<ul class='deal_logo' style='margin-bottom: 10px;'><li class='mui-col-sm-4 mui-col-xs-4'style='text-align: left;'><img style='height:18px;' src='images/US_40_29.png' alt='' class='mui-pull-left'><span>" + codeName + "</span><br><span style='margin-left:20px;color:#515252;'>" + stockCode + "</span></li><li class='mui-col-sm-2 mui-col-xs-2' style='text-align: center;'><span>" + count + "</span><br><span style='color:#515252;'>" + currency + "<span></li><li style='text-align: center' class='mui-col-sm-2 mui-col-xs-2' style='text-align: center;'><span style='color:#DAAC32'>" + floatFee + "</span><br><span style='color:#515252;'>" + floatRatio + "<span></li><li style='text-align:center;' class='mui-col-sm-4 mui-col-xs-4'><span>未成交</span><br><span style='color:#515252'>" + newPrice + "</span></li></ul>");
			}
			}
		})
	}else if(i==2){
		/******************持仓***************************/
		jQuery.ajax({
			type: 'post',
			url: server.stockCount,
			data: {
			account: account
			},
			dataType: 'json', //数据格式类型
			success: function(data) {
				//处理成功返回的数据
				//console.log(JSON.stringify(data));
				for(i = 0; i < data.data.length; i++) {
					var codeName = data.data[i].codeName;
					var stockCode = data.data[i].stockCode;
					var count = data.data[i].count;
					var floatFee = data.data[i].floatFee;
					var floatRatio = data.data[i].floatRatio;
					var occupyDeposit = data.data[i].occupyDeposit;
					var countCost = data.data[i].countCost;
					var newPrice = data.data[i].newPrice;
					var currency = data.data[i].currency;
					var count = data.data[i].count;
					var floatFee = data.data[i].floatFee;
					var _src,_tradeType;
					document.getElementById("price1").innerHTML = floatFee;
					document.getElementById("price2").innerHTML = floatFee;
					document.getElementById("price3").innerHTML = floatFee;
					   switch(data.data[i].tradingType){
	                	case 0:
	                		_tradeType = '买';break;
	                	case 1:
	                		_tradeType = '卖';break;
	                	default:
	                		_tradeType = '未知';break;
		            }
		            switch(data.data[i].currency){
	                	case 1:
	                		_src = '../images/HK_20x15_@3x.png';break;
	                	case 2:
	                		_src = '../images/US_40_29.png';break;
	                	default:
	                		_src = '';break;
	                }
				jQuery("#day_lists").append("<ul class='deal_logo' style='margin-bottom: 10px;'><li class='mui-col-sm-4 mui-col-xs-4'style='text-align: left;'><img style='height:18px;' src='"+_src+"' alt='' class='mui-pull-left'><span>" + codeName + "</span><br><span style='margin-left:20px;color:#515252;'>" + stockCode + "</span></li><li class='mui-col-sm-2 mui-col-xs-2' style='text-align: center;'><span>" + count + "</span></li><li style='text-align: center' class='mui-col-sm-2 mui-col-xs-2' style='text-align: center;'><span style='color:#DAAC32'>" + floatFee + "</span><br><span style='color:#515252;'>" + floatRatio + "<span></li><li style='text-align:center;' class='mui-col-sm-4 mui-col-xs-4'><span>" + countCost + "</span><br><span style='color:#515252'>" + newPrice + "</span></li></ul>");
				}
			}
		})
	}
	
});
/*********************点击撤单************************/
mui(".cocle").on('tap', 'ul', function() {
		jQuery.ajax({
			type: 'post',
			url: server.stockDay,
			data: {
			account: account,
			billStatus:2
			},
			dataType: 'json', //数据格式类型
			success: function(data) {
				//处理成功返回的数据
		//console.log(JSON.stringify(data));
		for(i = 0; i < data.data.length; i++) {
			var codeName = data.data[i].codeName;
			var stockCode = data.data[i].stockCode;
			var count = data.data[i].count;
			var floatFee = data.data[i].floatFee;
			var floatRatio = data.data[i].floatRatio;
			var occupyDeposit = data.data[i].occupyDeposit;
			var countCost = data.data[i].countCost;
			var newPrice = data.data[i].newPrice;
			var currency = data.data[i].currency;
			var count = data.data[i].count;
			var floatFee = data.data[i].floatFee;
			document.getElementById("price1").innerHTML = floatFee;
			document.getElementById("price2").innerHTML = floatFee;
			document.getElementById("price3").innerHTML = floatFee;
			jQuery("#day_lis").append("<ul class='deal_logo' style='margin-bottom: 10px;'><li class='mui-col-sm-4 mui-col-xs-4'style='text-align: left;'><img style='height:18px;' src='images/US_40_29.png' alt='' class='mui-pull-left'><span>" + codeName + "</span><br><span style='margin-left:20px;color:#515252;'>" + stockCode + "</span></li><li class='mui-col-sm-2 mui-col-xs-2' style='text-align: center;'><span>" + count + "</span><br><span style='color:#515252;'>" + currency + "<span></li><li style='text-align: center' class='mui-col-sm-2 mui-col-xs-2' style='text-align: center;'><span style='color:#DAAC32'>" + floatFee + "</span><br><span style='color:#515252;'>" + floatRatio + "<span></li><li style='text-align:center;' class='mui-col-sm-4 mui-col-xs-4'><span>未成交</span><br><span style='color:#515252'>" + newPrice + "</span></li></ul>");
			}
			}
		})
})
/************出金********************/
mui("#sub_money").on('tap', 'button', function() {
		var cardType=document.getElementById("m1").innerHTML
		var cardNo=document.getElementById("m2").innerHTML
		//var usdFee=document.getElementById("m3").innerHTML
		var usdFee=document.getElementById("m4").value;
		
		jQuery.ajax({
			type: 'post',
			url: server.stockApply,
			data: {
			account: account,
			type:1, 
			cardNo:cardNo,
			cardType:cardType,
			usdFee:usdFee
			},
			dataType: 'json', //数据格式类型
			success: function(data) {
				//处理成功返回的数据
				console.log(JSON.stringify(data));
				
			}
		})
})
/*******************入金******************************/
mui("#h_money").on('tap', 'button', function() {
		var have=document.getElementById("h").value;
		jQuery.ajax({
			type: 'post',
			url: server.stockApply,
			data: {
			account: account,
			type:0, 
			usdFee:have
			},
			dataType: 'json', //数据格式类型
			success: function(data) {
				//处理成功返回的数据
				console.log(JSON.stringify(data));
				
			}
		})
})
var _codeType = ''; //股票类型 2 港股 3美股
jQuery(function() {
	var href = location.href;
	var code = href.split("?code=")[1] || '';
	jQuery.ajax({
		type: 'post',
		url: server.stockOne,
		data: {
			stockCode: code,
			account: account
		},
		dataType: 'json',
		success: function(data) {
			if(data.code == 0) {
				console.log(JSON.stringify(data));
				var code = data.data[0].code;
				var name = data.data[0].name;
				var change = data.data[0].change;
				var changeRate = data.data[0].changeRate;
				var closePrice = data.data[0].closePrice;
				var price = data.data[0].price;
				var volume = data.data[0].volume;
				var num = 1;
				document.getElementById("p1").innerHTML = name;
				document.getElementById("p2").innerHTML = change;
				document.getElementById("p3").innerHTML = changeRate + "%";
				document.getElementById("hi").innerHTML = price;
				document.getElementById("lo").innerHTML = volume;
				document.getElementById("mony").value = price;
				document.getElementById("in").value = num;
				document.getElementById("stoke_num").value = code;
				_codeType = data.data[0].type;
			} else {

			}

		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			maskHideEvent()
			console.log(type);
		}
	})
});
/**************************买入列表*******************************/
jQuery(function() {
		jQuery.ajax({
			type: 'post',
			url: server.stockCount,
			data: {
			account: account
			},
			dataType: 'json', //数据格式类型
			success: function(data) {
				//处理成功返回的数据
				//console.log(JSON.stringify(data));
				for(i = 0; i < data.data.length; i++) {
					var codeName = data.data[i].codeName;
					var stockCode = data.data[i].stockCode;
					var count = data.data[i].count;
					var floatFee = data.data[i].floatFee;
					var floatRatio = data.data[i].floatRatio;
					var occupyDeposit = data.data[i].occupyDeposit;
					var countCost = data.data[i].countCost;
					var newPrice = data.data[i].newPrice;
					var currency = data.data[i].currency;
					var count = data.data[i].count;
					var floatFee = data.data[i].floatFee;
					var _src,_tradeType;
					document.getElementById("price1").innerHTML = floatFee;
					document.getElementById("price2").innerHTML = floatFee;
					document.getElementById("price3").innerHTML = floatFee;
					   switch(data.data[i].tradingType){
	                	case 0:
	                		_tradeType = '买';break;
	                	case 1:
	                		_tradeType = '卖';break;
	                	default:
	                		_tradeType = '未知';break;
		            }
		            switch(data.data[i].currency){
	                	case 1:
	                		_src = '../images/HK_20x15_@3x.png';break;
	                	case 2:
	                		_src = '../images/US_40_29.png';break;
	                	default:
	                		_src = '';break;
	                }
					 //alert(_src );
				jQuery("#day_list").append("<ul class='deal_logo' style='margin-bottom: 10px;'><li class='mui-col-sm-4 mui-col-xs-4'style='text-align: left;'><img style='height:18px;' src='"+_src+"' alt='' class='mui-pull-left'><span>" + codeName + "</span><br><span style='margin-left:20px;color:#515252;'>" + stockCode + "</span></li><li class='mui-col-sm-2 mui-col-xs-2' style='text-align: center;'><span>" + count + "</span></li><li style='text-align: center' class='mui-col-sm-2 mui-col-xs-2' style='text-align: center;'><span style='color:#DAAC32'>" + floatFee + "</span><br><span style='color:#515252;'>" + floatRatio + "<span></li><li style='text-align:center;' class='mui-col-sm-4 mui-col-xs-4'><span>" + countCost + "</span><br><span style='color:#515252'>" + newPrice + "</span></li></ul>");
				}
			}
		})
})
mui('.select_total').on('tap', '#buy', function() {
	var ths = jQuery(this);
	ths.addClass('tradeAct');
	ths.siblings().removeClass('tradeAct');
	var title = document.getElementById('p1').innerHTML;
	var price = document.getElementById('p1').innerHTML;
	var code = document.getElementById("stoke_num").value;
	var name = document.getElementById("p1").innerHTML;
	var num = document.getElementById("in").value;
	var type = document.getElementById("in").value;
	document.getElementById("title").innerHTML = title;
	document.getElementById("title4").innerHTML = code;
	document.getElementById("title1").innerHTML = price;
	document.getElementById("title2").innerHTML = num;
	document.getElementById("title3").innerHTML = num;
	jQuery("#tost").show();
});
mui('.select_total').on('tap', '#sale', function() {
	var ths = jQuery(this);
	ths.addClass('tradeAct');
	ths.siblings().removeClass('tradeAct');
	var title = document.getElementById('p1').innerHTML;
	var price = document.getElementById('p1').innerHTML;
	var code = document.getElementById("stoke_num").value;
	var name = document.getElementById("p1").innerHTML;
	var num = document.getElementById("in").value;
	var type = document.getElementById("in").value;
	document.getElementById("title").innerHTML = title;
	document.getElementById("title4").innerHTML = code;
	document.getElementById("title1").innerHTML = price;
	document.getElementById("title2").innerHTML = num;
	document.getElementById("title3").innerHTML = num;
	jQuery("#tost").show();
});
mui('#tost').on('tap', '#btn1', function() {
	var code = document.getElementById("stoke_num").value;
	var pic = document.getElementById("hi").innerHTML;
	var num = document.getElementById("in").value;
	//var market=document.getElementById("price").innerHTML;
	var _billType = jQuery('.priceType option:selected').val();
	var _type = jQuery('.select_total .tradeAct').attr('data-type');
	var _data = {
		account: account,
		tradingType: _type,
		entrustCount: num,
		price: pic,
		stockCode: code,
		currency: _codeType == 2 ? 1 : 2,
		billType: _billType
	};
	dealEve(_data);
})
mui('#tost').on('tap', '#btn2', function() {
	jQuery("#tost").hide();
})

function dealEve(_data) {
	jQuery.ajax({
		type: 'post',
		url: server.stockABill,
		data: _data,
		beforeSend: function() {
			console.log(_data);
		},
		success: function(result) {
			mui.toast(result.msg);
			jQuery("#tost").hide();
		},
		error: function(result) {
			console.log(result);
		}
	})
}
/**********************持仓************************/


document.getElementById("title_trade").addEventListener('tap', function() {
	mui.openWindow({
		url: 'dayTrade.html',
		id: 'dayTrade.html'
	});
});
document.getElementById("title_commissio").addEventListener('tap', function() {
	mui.openWindow({
		url: 'dayCommissio.html',
		id: 'dayCommissio.html'
	});
});
document.getElementById("history_trade").addEventListener('tap', function() {
	mui.openWindow({
		url: 'historyTrad.html',
		id: 'historyTrad.html'
	});
});
document.getElementById("history_commissio").addEventListener('tap', function() {
	mui.openWindow({
		url: 'historyCommissio.html',
		id: 'historyCommissio.html'
	});
});
document.getElementById("deposit").addEventListener('tap', function() {
	mui.openWindow({
		url: 'deposit.html',
		id: 'deposit.html'
	});
});
document.getElementById("gold").addEventListener('tap', function() {
	mui.openWindow({
		url: 'gold.html',
		id: 'gold.html'
	});
});
/*****************************************撤单*********************************************/
/****************************************持仓********************************************/

/****************************撤单******持仓**************************************/
mui.ajax(server.stockMdetail, {
	data: {
		account: account,
		billStatus: 2
	},
	dataType: 'json', //数据格式类型
	type: 'POST', //http请求类型
	timeout: 10000, //超时设置
	success: function(data) {
		//处理成功返回的数据
		console.log(data);
		for(i = 0; i < data.data.length; i++) {
			var codeName = data.data[i].codeName;
			var stockCode = data.data[i].stockCode;
			var count = data.data[i].count;
			var floatFee = data.data[i].floatFee;
			var floatRatio = data.data[i].floatRatio;
			var occupyDeposit = data.data[i].occupyDeposit;
			var countCost = data.data[i].countCost;
			var newPrice = data.data[i].newPrice;
			var currency = data.data[i].currency;
			var count = data.data[i].count;
			var floatFee = data.data[i].floatFee;
			document.getElementById("price1").innerHTML = floatFee;
			document.getElementById("price2").innerHTML = floatFee;
			document.getElementById("price3").innerHTML = floatFee;
			jQuery("#day_lis").append("<ul class='deal_logo' style='margin-bottom: 10px;'><li class='mui-col-sm-4 mui-col-xs-4'style='text-align: left;'><img style='height:18px;' src='images/US_40_29.png' alt='' class='mui-pull-left'><span>" + codeName + "</span><br><span style='margin-left:20px;color:#515252;'>" + stockCode + "</span></li><li class='mui-col-sm-2 mui-col-xs-2' style='text-align: center;'><span>" + count + "</span><br><span style='color:#515252;'>" + currency + "<span></li><li style='text-align: center' class='mui-col-sm-2 mui-col-xs-2' style='text-align: center;'><span style='color:#DAAC32'>" + floatFee + "</span><br><span style='color:#515252;'>" + floatRatio + "<span></li><li style='text-align:center;' class='mui-col-sm-4 mui-col-xs-4'><span>未成交</span><br><span style='color:#515252'>" + newPrice + "</span></li></ul>");
		}
	}
})

/*mui.ajax(server.stockBill, {
	data: {
		account: account,
		billStatus: 2
	},
	dataType: 'json', //数据格式类型
	type: 'POST', //http请求类型
	timeout: 10000, //超时设置
	success: function(data) {
		//处理成功返回的数据
		console.log(JSON.stringify(data));
		for(i = 0; i < data.data.length; i++) {
			var codeName = data.data[i].codeName;
			var stockCode = data.data[i].stockCode;
			var count = data.data[i].count;
			var floatFee = data.data[i].floatFee;
			var floatRatio = data.data[i].floatRatio;
			var occupyDeposit = data.data[i].occupyDeposit;
			var countCost = data.data[i].countCost;
			var newPrice = data.data[i].newPrice;
			var currency = data.data[i].currency;
			var count = data.data[i].count;
			var floatFee = data.data[i].floatFee;
			document.getElementById("price1").innerHTML = floatFee;
			document.getElementById("price2").innerHTML = floatFee;
			document.getElementById("price3").innerHTML = floatFee;
			jQuery("#day_lists").append("<ul class='deal_logo' style='margin-bottom: 10px;'><li class='mui-col-sm-4 mui-col-xs-4'style='text-align: left;'><img style='height:18px;' src='images/US_40_29.png' alt='' class='mui-pull-left'><span>" + codeName + "</span><br><span style='margin-left:20px;color:#515252;'>" + stockCode + "</span></li><li class='mui-col-sm-2 mui-col-xs-2' style='text-align: center;'><span>" + count + "</span><br><span style='color:#515252;'>" + currency + "<span></li><li style='text-align: center' class='mui-col-sm-2 mui-col-xs-2' style='text-align: center;'><span style='color:#DAAC32'>" + floatFee + "</span><br><span style='color:#515252;'>" + floatRatio + "<span></li><li style='text-align:center;' class='mui-col-sm-4 mui-col-xs-4'><span>未成交</span><br><span style='color:#515252'>" + newPrice + "</span></li></ul>");
		}
	}
})*/

mui("#cocle").on('tap', 'ul', function() {
	var ths = jQuery(this);
	var i = jQuery(this).index();
	var val = ths.find("li:nth-child(2)").text(); //注意：这里的下标从一开始，或者使用:first-child  
	document.getElementById("stoke_num").value = val;
	document.getElementById("scroll").style.display = 'none';
});